package com.jiangjiang.eshop.logistics.controller;

import com.jiangjiang.eshop.logistics.api.LogisticApi;
import com.jiangjiang.eshop.logistics.api.QueryProgressRequest;
import com.jiangjiang.eshop.logistics.api.QueryProgressRequestBuilder;
import com.jiangjiang.eshop.logistics.api.QueryProgressResponse;
import com.jiangjiang.eshop.logistics.dto.LogisticTraceQueryDTO;
import com.jiangjiang.eshop.wms.service.WmsFacadeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName LogisticController
 * @Description TODO
 * @Author jhw
 * @Date 2021/9/25 23:27
 * @Version 1.0
 **/
@Slf4j
@RestController
@RequestMapping("/logistics")
public class LogisticController {

    @Autowired
    private WmsFacadeService wmsFacadeService;
    @Autowired
    private LogisticApi logisticApi;

    /**
     * 获取订单的物流进度
     *
     * @param dto 查询参数
     * @return 查询结果
     */
    @PostMapping("/getLogisticsProgress")
    public List<QueryProgressResponse.TraceEvent> getLogisticsProgress(@RequestBody LogisticTraceQueryDTO dto) {
        try {
            QueryProgressRequest request = QueryProgressRequestBuilder.get()
                    .buildOrderNo(dto.getOrderNo())
                    .buildLogisticCode(wmsFacadeService.getLogisticCode(dto.getOrderId()))
                    .create();

            QueryProgressResponse queryProgressResponse = logisticApi.queryProgress(request);
            return queryProgressResponse.getTraceEvents();
        } catch (Exception e) {
            log.warn("LogisticController getLogisticsProgress error", e);
        }
        return new ArrayList<>();
    }

}
